﻿using System;
using System.Collections.Generic;

using aier.dataaccess.model;
using aier.dataaccess.dapper;
using aier.wx.nosql.redis;
using aier.xrm.wcf;

using Microsoft.Xrm.Sdk;

namespace aier.business.logic
{
    public class StreetBll
    {
        public StreetDal streetDal { get; set; }
        public CommonWcf commonWcf { get; set; }

        public RedisTestDal redisTestDal { get; set; }


        public Tuple<Guid?,Guid?,Guid?> GetPCAByName(string provinceName,string cityName,string areaName ) {
           return streetDal.GetPCAByName(provinceName, cityName,areaName);
        }

        /// <summary>
        /// 通过视图获取所有街道信息
        /// </summary>
        /// <returns></returns>
        public List<StreetView> QueryStreetByProviceCityArea(string provinceName, string cityName, string areaName,string hospital)
        {
            // string keyName = string.Concat(provinceName,cityName,areaName);
            List<StreetView> streetViews = null;
            //  streetViews = redisTestDal.Get<List<StreetView>>(keyName);

            //if (streetViews != null && streetViews.Count > 0)
            //{
            //    return streetViews;
            //}
            //else
            //{
            cityName = cityName == "全部" ? string.Empty : cityName;
            areaName = areaName == "全部" ? string.Empty : areaName;
            streetViews = streetDal.QueryStreetByProvinceCityArea(provinceName, cityName, areaName, hospital);
            //    redisTestDal.Set<List<StreetView>>(keyName, streetViews);
            //}

            return streetViews;
        }

        /// <summary>
        /// 根据街道ID查询视图信息
        /// </summary>
        /// <param name="shoolId"></param>
        /// <returns></returns>
        public StreetView QueryStreetById(string shoolId)
        {
            //return new CommunityDal().QueryCommunityById(communityId);
            return streetDal.QueryStreetId(shoolId);
        }

        /// <summary>
        /// 根据街道名称查询视图信息
        /// </summary>
        /// <param name="StreetName"></param>
        /// <returns></returns>
        public List<StreetView> QueryStreetByName(string streetName, string provinceName, string cityName, string areaName)
        {
            cityName = cityName == "全部" ? string.Empty : cityName;
            areaName = areaName == "全部" ? string.Empty : areaName;
            return streetDal.QueryStreetByName(streetName, provinceName, cityName, areaName);
        }

        /// <summary>
        /// 根据省市区/县ID查询街道视图信息
        /// </summary>
        /// <param name="provice"></param>
        /// <param name="city"></param>
        /// <param name="county"></param>
        /// <returns></returns>
        public List<StreetView> QueryStreetByProviceCityCounty(string provice, string city, string county)
        {
            return streetDal.QueryStreetByProviceCityCounty( provice,  city,  county);
        }

        /// <summary>
        /// 根据区县查所属街道 
        /// 原程序 LocationQueryServiceImpl/queryStreet
        /// </summary>
        /// <param name="areaid">区县ID</param>
        /// <returns></returns>
        public List<StreetView> QueryStreetByAreaId(Guid areaid)
        {
            List<StreetView> streetViews = null;
            streetViews = streetDal.QueryStreetByAreaId(areaid);
            return streetViews;
        }
        /// <summary>
        /// 根据不同条件查询街道数量,只要属性不为空就视为查询条件
        /// 查询条件为：省份ID/市ID/区ID/街道ID/名称
        /// 原程序 LocationQueryServiceImpl/queryStreetCount
        /// </summary>
        /// <param name="streetView">街道对象</param>
        /// <returns></returns>
        public int QueryStreetCount(StreetView streetView)
        {
            int count = streetDal.QueryStreetCount(streetView);
            return count;
        }

        /// <summary>
        /// 根据街道ID修改街道信息
        /// </summary>
        /// <param name="street"></param>
        /// <returns></returns>
        public void UpdateStreetById(Street street)
        {
            Entity entity = XrmSqlBuilderEx.AsCrmEntity<Street>(street);

            commonWcf.UpdateEntityById(entity);
            //new CommunityWcf().UpdateCommunityById(entity);
        }

        /// <summary>
        /// 创建街道
        /// </summary>
        /// <param name="entity"></param>
        public Guid CreateStreet(Street street)
        {
            var pcaid =  streetDal.GetPCAByName(street?.provinceName, street?.cityName, street?.areaName);
            street.new_province = pcaid.Item1;
            street.new_city = pcaid.Item2;
            street.new_area = pcaid.Item3;
            if(street.new_province!=null && street.new_city!=null && street.new_area!=null)
            {
                Entity entity = XrmSqlBuilderEx.AsCrmEntity<Street>(street);

                return commonWcf.CreateEntity(entity);
            }
            throw new Exception("省市区县没有找到对应得记录");
            //return new CommunityWcf().CreateCommuntity(entity);
        }

        /// <summary>
        ///更新街道
        /// </summary>
        /// <param name="entity"></param>
        public void UpdateStreet(Street Street)
        {
            Entity entity = XrmSqlBuilderEx.AsCrmEntity<Street>(Street);

             commonWcf.UpdateEntityById(entity);
            //return new CommunityWcf().CreateCommuntity(entity);
        }
    }
}
